<?php
	/**
	 +=========================================================
	 * @copyright © 2011 focalthinker.com All rights reserved.
	 * @author NickDraw(零度温柔) webmaster@206c.net
	 * @license http://www.206c.com/license
	 +=========================================================
	 * 角色权限操作类 
	 * RoleAction
	 +=========================================================
	 */
	class RoleAction extends CommonAction {
		
		public function index()
		{
			$model = M('role');
			$data = $model->where("pid='0'")->select();
			
			$html = '';
			$i = 1;
			foreach ($data as $key=>$val){
				$trcolor = '';
				if ( ( $i % 2 ) == 0 ){
					$trcolor = ' bgcolor="#f9f9f9"';
				}
				$html .= '<tr rel="' . $val['id'] . '" class="row_color"'.$trcolor.'>' .
					'<td align="center">' . $val['id'] . '</td>' .
					'<td align="left"><a href="' . U('edit?id='.$val['id']) . '" class="edit_role">' . $val['name'] . '</a></td>' . 
					'<td align="center">' . getGroupName($val['pid']) . '</td>' . 
					'<td align="center">' . getStatus($val['status']) . '</td>' .
					'<td align="center">' . $val['remark'] . '</td>' .
					'<td class="action_e">'.
					showStatus($val['status'],$val['id']) . '<span class="action_line">|</span>'.
					'<a href="' . U('app?groupId='.$val['id']) . '" title="' . $val['name'] . ' 授权 " class="role_action">授权</a><span class="action_line">|</span>'.
					'<a href="' . U('user?id='.$val['id']) . '" title="' . $val['name'] . ' 用户列表 " class="role_user_list">用户列表</a><span class="action_line">|</span>'.
					'<a href="' . U('delete?id='.$val['id']) . '" title="' . $val['name'] . ' 删除 " rel="' . $val['id'] . '" class="del_role">删除</a>'.
					'</td>'.
				'</tr>';
				
				$html .= $this->getSonRole($model, $val['id'], '├──');
				$i++;
			}
			//$this->assign("list",$data);
			$this->assign("list",$html);
			Cookie::set ( '_currentUrl_', __SELF__ );
			$this->display();
		}
		
		//检查是否有子级内容
		public function checkDelete()
		{
			$model = M('role');
			$data = $model->where("pid='".$_GET['id']."'")->select();
			if (!empty($data)){
				$this->ajaxReturn('','存在子级角色，需先删除子级角色！',0);
			}else{
				$this->ajaxReturn('','',1);
			}
		}
		
		protected function getSonRole($model,$pid,$step){
			$data = $model->where("pid='".$pid."'")->select();
			$html = '';
			if (!empty($data)){
				foreach ($data as $key=>$val){
					$html .= '<tr rel="' . $val['id'] . '">' .
						'<td align="center">' . $val['id'] . '</td>' .
						'<td align="left" style="color:#999;">' . $step . ' <a href="' . U('edit?id='.$val['id']) . '" class="edit_role">' . $val['name'] . '</a></td>' . 
						'<td align="center">' . getGroupName($val['pid']) . '</td>' . 
						'<td align="center">' . getStatus($val['status']) . '</td>' .
						'<td align="center">' . $val['remark'] . '</td>' .
						'<td class="action_e">'.
						showStatus($val['status'],$val['id']) . '<span class="action_line">|</span>'.
						'<a href="' . U('app?groupId='.$val['id']) . '" title="' . $val['name'] . ' 授权 " class="role_action">授权</a><span class="action_line">|</span>'.
						'<a href="' . U('user?id='.$val['id']) . '" title="' . $val['name'] . ' 用户列表 " class="role_user_list">用户列表</a><span class="action_line">|</span>'.
						'<a href="' . U('delete?id='.$val['id']) . '" title="' . $val['name'] . ' 删除 " rel="' . $val['id'] . '" class="del_role">删除</a>'.
						'</td>'.
					'</tr>';
					
					$html .= $this->getSonRole($model, $val['id'], '<span style="color:#ccc;">├──</span> '.$step);
				}
			}
			return $html;
		}
		
		/**
	     +----------------------------------------------------------
	     * 组操作权限列表
	     *
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function app()
	    {
	        //读取系统的项目列表
	        $node    =  D("Node");
	        $list	=	$node->where('level=1')->field('id,title')->select();
			foreach ($list as $vo){
				$appList[$vo['id']]	=	$vo['title'];
			}
	
	        //读取系统组列表
			$group   =  D('Role');
	        $list       =  $group->field('id,name')->select();
			foreach ($list as $vo){
				$groupList[$vo['id']]	=	$vo['name'];
			}
			
			$this->assign("groupList",$groupList);
	
	        //获取当前用户组项目权限信息
	        $groupId =  isset($_GET['groupId']) ? $_GET['groupId'] : '';
			$groupAppList = array();
			if(!empty($groupId)) {
				$this->assign("selectGroupId",$groupId);
				//获取当前组的操作权限列表
	            $list	=	$group->getGroupAppList($groupId);
				foreach ($list as $vo){
					$groupAppList[$vo['id']]	=	$vo['id'];
				}
			}
			$this->assign('groupAppList',$groupAppList);
	        $this->assign('appList',$appList);
	        $this->display();
	
	        return;
	    }
		
		/**
	     +----------------------------------------------------------
	     * 组操作权限列表
	     *
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function module()
	    {
	        $groupId =  $_GET['groupId'];
	        $appId  = $_GET['appId'];
	
			$group   =  D("Role");
	        //读取系统组列表
	        $list=$group->field('id,name')->select();
			foreach ($list as $vo){
				$groupList[$vo['id']]	=	$vo['name'];
			}
			$this->assign("groupList",$groupList);
	
	        if(!empty($groupId)) {
				$this->assign("selectGroupId",$groupId);
	            //读取系统组的授权项目列表
	            $list	=	$group->getGroupAppList($groupId);
				foreach ($list as $vo){
					$appList[$vo['id']]	=	$vo['title'];
				}
	            $this->assign("appList",$appList);
	        }
	        $node    =  D("Node");
	        if(!empty($appId)) {
	            $this->assign("selectAppId",$appId);
	        	//读取当前项目的模块列表
				$where['level']=2;
				$where['pid']=$appId;
	            $nodelist=$node->field('id,title')->where($where)->select();
				foreach ($nodelist as $vo){
					$moduleList[$vo['id']]	=	$vo['title'];
				}
	        }
	
	        //获取当前项目的授权模块信息
			$groupModuleList = array();
			if(!empty($groupId) && !empty($appId)) {
	            $grouplist	=	$group->getGroupModuleList($groupId,$appId);
				foreach ($grouplist as $vo){
					$groupModuleList[$vo['id']]	=	$vo['id'];
				}
			}
	
			$this->assign('groupModuleList',$groupModuleList);
	        $this->assign('moduleList',$moduleList);
	
	        $this->display();
	
	        return;
	    }

		/**
	     +----------------------------------------------------------
	     * 组操作权限列表
	     *
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function action()
	    {
	        $groupId =  $_GET['groupId'];
	        $appId  = $_GET['appId'];
	        $moduleId  = $_GET['moduleId'];
	
			$group   =  D("Role");
	        //读取系统组列表
	        $grouplist=$group->field('id,name')->select();
			foreach ($grouplist as $vo){
				$groupList[$vo['id']]	=	$vo['name'];
			}
			$this->assign("groupList",$groupList);
	
	        if(!empty($groupId)) {
				$this->assign("selectGroupId",$groupId);
	            //读取系统组的授权项目列表
	            $list	=	$group->getGroupAppList($groupId);
				foreach ($list as $vo){
					$appList[$vo['id']]	=	$vo['title'];
				}
	            $this->assign("appList",$appList);
	        }
	        if(!empty($appId)) {
	            $this->assign("selectAppId",$appId);
	        	//读取当前项目的授权模块列表
	            $list	=	$group->getGroupModuleList($groupId,$appId);
				foreach ($list as $vo){
					$moduleList[$vo['id']]	=	$vo['title'];
				}
	            $this->assign("moduleList",$moduleList);
	        }
	        $node    =  D("Node");
	
	        if(!empty($moduleId)) {
	            $this->assign("selectModuleId",$moduleId);
	        	//读取当前项目的操作列表
				$map['level']=3;
				$map['pid']=$moduleId;
	            $list	=	$node->where($map)->field('id,title')->select();
				if($list) {
					foreach ($list as $vo){
						$actionList[$vo['id']]	=	$vo['title'];
					}
				}
	        }
	
	
	        //获取当前用户组操作权限信息
			$groupActionList = array();
			if(!empty($groupId) && !empty($moduleId)) {
				//获取当前组的操作权限列表
	            $list	=	$group->getGroupActionList($groupId,$moduleId);
				if($list) {
				foreach ($list as $vo){
					$groupActionList[$vo['id']]	=	$vo['id'];
				}
				}
	
			}
	
			$this->assign('groupActionList',$groupActionList);
			//$actionList = array_diff_key($actionList,$groupActionList);
	        $this->assign('actionList',$actionList);
	
	        $this->display();
	
	        return;
	    }

		public function navigation(){
			$group_model = M('group');
			$group_list = $group_model->where("pid=0")->select();
			$this->assign("group_list",$group_list);
			
			$group_access = M('access_menu');
			$group_access_list = $group_access->where("role_id='".$_GET['groupId']."'")->select();
			$this->assign("group_access",$group_access_list);
			
			$this->display();
		}

		public function setGroupNavigation(){
			$model = D('access_menu');
			if (!empty($_POST['group_list'])){
				$model->where("role_id='".$_POST['role_id']."'")->delete();
				foreach($_POST['group_list'] as $key=>$val){
					$map['role_id'] = $_POST['role_id'];
					$map['group_id'] = $val;
					$model->add($map);
				}
				$this->success("授权完成");
			}
		}
		
		
		/**
	     +----------------------------------------------------------
	     * 组操作权限列表
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function user()
	    {
	        //读取系统的用户列表
	        $user    =   D("User");
			$list2=$user->field('id,account,nickname')->select();
			//echo $user->getlastsql();
			//dump(	$user);
			foreach ($list2 as $vo){
				$userList[$vo['id']]	=	$vo['account'].' ('.$vo['nickname'].')';
			}
	
			$group    =   D("Role");
	        $list=$group->field('id,name')->select();
			foreach ($list as $vo){
				$groupList[$vo['id']]	=	$vo['name'];
			}
			$this->assign("groupList",$groupList);
	
	        //获取当前用户组信息
	        $groupId =  isset($_GET['id'])?$_GET['id']:'';
			$groupUserList = array();
			if(!empty($groupId)) {
				$this->assign("selectGroupId",$groupId);
				//获取当前组的用户列表
	            $list	=	$group->getGroupUserList($groupId);
				foreach ($list as $vo){
					$groupUserList[$vo['id']]	=	$vo['id'];
				}
	
			}
			$this->assign('groupUserList',$groupUserList);
	        $this->assign('userList',$userList);
	        $this->display();
	
	        return;
	    }
		
		/**
	     +----------------------------------------------------------
	     * 增加组操作权限
	     *
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function setApp()
	    {
	        $id     = $_POST['groupAppId'];
			$groupId	=	$_POST['groupId'];
			$group    =   D('Role');
			$group->delGroupApp($groupId);
			$result = $group->setGroupApps($groupId,$id);
	
			if($result===false) {
				$this->error('项目授权失败！');
			}else {
				$this->success('项目授权成功！');
			}
	    }
		
		/**
	     +----------------------------------------------------------
	     * 增加组操作权限
	     *
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function setModule()
	    {
	        $id     = $_POST['groupModuleId'];
			$groupId	=	$_POST['groupId'];
	        $appId	=	$_POST['appId'];
			$group    =   D("Role");
			$group->delGroupModule($groupId,$appId);
			$result = $group->setGroupModules($groupId,$id);
	
			if($result===false) {
				$this->error('模块授权失败！');
			}else {
				$this->success('模块授权成功！');
			}
	    }
		
		/**
	     +----------------------------------------------------------
	     * 增加组操作权限
	     *
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function setAction()
	    {
	        $id     = $_POST['groupActionId'];
			$groupId	=	$_POST['groupId'];
	        $moduleId	=	$_POST['moduleId'];
			$group    =   D("Role");
			$group->delGroupAction($groupId,$moduleId);
			$result = $group->setGroupActions($groupId,$id);
	
			if($result===false) {
				$this->error('操作授权失败！');
			}else {
				$this->success('操作授权成功！');
			}
	    }
		
		/**
	     +----------------------------------------------------------
	     * 增加组操作权限
	     +----------------------------------------------------------
	     * @access public
	     +----------------------------------------------------------
	     * @return void
	     +----------------------------------------------------------
	     * @throws FcsException
	     +----------------------------------------------------------
	     */
	    public function setUser()
	    {
	        $id     = $_POST['groupUserId'];
			$groupId	=	$_POST['groupId'];
			$group    =   D("Role");
			$group->delGroupUser($groupId);
			$result = $group->setGroupUsers($groupId,$id);
			if($result===false) {
				$this->error('授权失败！');
			}else {
				$this->assign("jumpUrl",$_SERVER['HTTP_REFERER']);
				$this->success("操作成功！");
			}
	    }
		
		public function _before_edit(){
		   $Group = D('Role');
	        //查找满足条件的列表数据
	        $list     = $Group->field('id,name')->select();
	        $this->assign('list',$list);
	
		}
		public function _before_add(){
		   $Group = D('Role');
	        //查找满足条件的列表数据
	        $list     = $Group->field('id,name')->select();
	        $this->assign('list',$list);
	
		}
	    public function select()
	    {
	        $map = $this->_search();
	        //创建数据对象
	        $Group = D('Role');
	        //查找满足条件的列表数据
	        $list     = $Group->field('id,name')->select();
	        $this->assign('list',$list);
	        $this->display();
	        return;
	    }
		
	}
	
?>